package com.ziyun.pop.web.modules.erpSupplierOffline.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.utility.JsonUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.common.e.DeleteEnum;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.erpSupplierOffline.entity.ErpSupplierStateEnum;
import io.renren.common.validator.ValidatorUtils;
import javafx.beans.binding.ObjectExpression;
import org.apache.poi.ss.formula.ptg.MemAreaPtg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.pop.modules.erpSupplierOffline.entity.ErpSupplierOfflineEntity;
import com.ziyun.pop.modules.erpSupplierOffline.service.ErpSupplierOfflineService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 线下外协商
 *
 * @author llc
 * @email 787254039@qq.com
 * @date 2018-10-30 14:07:19
 */
@RestController
@RequestMapping("suppOffline/erpsupplieroffline")
public class ErpSupplierOfflineController {

    @Value("${erpApi}")
    private String erpApi ;

    @Autowired
    private ErpSupplierOfflineService erpSupplierOfflineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("erpSupplierOffline:erpsupplieroffline:list")
    public R list(@RequestParam Map<String, Object> params){
        String url = erpApi + "/suppOffline/erpsupplieroffline/list";
        String response = HttpUtils.doPost(url, params);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{supplierId}")
   @RequiresPermissions("erpSupplierOffline:erpsupplieroffline:info")
    public R info(@PathVariable("supplierId") Integer supplierId){
        Map<String,Object> params=new HashMap<>();
        params.put("supplierId", supplierId);
        String url = erpApi + "/suppOffline/erpsupplieroffline/info";
        String response = HttpUtils.doPost(url, params);
        R rs = JsonUtils.string2Object(response, R.class);

        return rs;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("erpSupplierOffline:erpsupplieroffline:save")
    public R save(@RequestBody ErpSupplierOfflineEntity erpSupplierOffline){
        //判断是否有重复的供应商名称或电话
        int count = erpSupplierOfflineService.selectCountBySupplierName(erpSupplierOffline.getSupplierName());
        if(count>0){
            return R.error("此供应商已存在，请勿重复添加!");
        }
        //判断是否有重复的供应商名称或电话
        int telCount = erpSupplierOfflineService.selectCountByTel(erpSupplierOffline.getContactTel());
        if(telCount>0){
            return R.error("此联系方式供应商已存在，请勿重复添加!");
        }

        erpSupplierOffline.setCreateName(UserUtils.getCurentUser().getUsername());
        erpSupplierOffline.setCreateTime(new Date());
        erpSupplierOffline.setAuditName(UserUtils.getCurentUser().getUsername());
        erpSupplierOffline.setAuditTime(new Date());
        erpSupplierOffline.setState(ErpSupplierStateEnum.IS_AUDITED.toInt());
        erpSupplierOfflineService.save(erpSupplierOffline);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("erpSupplierOffline:erpsupplieroffline:update")
    public R update(@RequestBody Map<String,Object> map){
        map.put("updateName",UserUtils.getCurentUser().getUsername());
        return erpSupplierOfflineService.updateByMap(map);//全部更新
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("erpSupplierOffline:erpsupplieroffline:delete")
    public R delete(@RequestBody Integer[] supplierIds){
        erpSupplierOfflineService.removeByIds(Arrays.asList(supplierIds));
        return R.ok();
    }

}
